<?PHP

class VoucherBO extends BusinessObject{
    
  var $pdf;
  var $top_y;
  var $a4_w  = 595;   
  var $a4_h  = 841;  
  var $default_voucher = 157;
  
  /* private function using in constructors*/
  function setAttributes(){
    $this->fields   = array(
			"id"                =>	"voucher_id",
			"title"             =>	"voucher_title",
			"content" 	        =>	"voucher_content",
			"vtype" 	        =>	"voucher_type"			 
		);
    BusinessObject::setAttributes();
  }
  /* constuctor */
  function VoucherBO(){
    /** super(tableName) */
    BusinessObject::BusinessObject("vouchers");
    $this->setAttributes();
  }
  
  function getOrderVouchersAsHTML($order_id,$user_id,$order_part_id="")
  {
      $HTML = "";
      $main_provider_id = 1;
      
      $user       = new UserBO();

      $is_agent   = false;
      $agent_name = "-";
      $user->id = $user_id;
      $user->Load(0,1);
      if($user->Next())
      {
         if($user->agn_agent==1)
         {
             $is_agent = true;
             $agent_name = $user->fname." ".$user->lname;             
         }
      }
      
      $order         = new OrderBO();
      $order->id     = $order_id;
      if($is_agent)
      {
        $order->agentID = $user_id;  
      }else
      {
        $order->user   = $user_id;    
      }      
      //$order->status = $order->STATUS_PAID;
      $order->Load(0,1);

    $affiliate_name = "";

    if($order->Next())
    {
      if($order->affiliate_id !="0")
      {
        $affiliate = new AffiliateBO();
        $affiliate->id  = $order->affiliate_id;

        $affiliate->Load(0,1);
        if($affiliate->Next())
        {
	      $affiliate_name = $affiliate->firstName." ".$affiliate->lastName;
        }
      }

      $total = 0;
      if($is_agent) //if agent - recalculate total amount for Voucher
      {
        $order_parts = new OrderPartBO();
        $order_parts->order = $order_id;
        if($order_part_id!="") $order_parts->id = $order_part_id;
        $order_parts->Load(0,0);

        while($order_parts->Next())
        {
            $item = new ItemBO();
            $item->id = $order_parts->item;    
            $item->Load(0,0);    
            $item->Next();
            $total += $order_parts->adults_count * $item->adult_price + $order_parts->childs_count * $item->child_price;
        }
      }else
      {
         $total = $order->total;
      }
      
      $vtmpl = new Template();
      
      $main_provider_voucher = "";
      $main_provider_voucher_items_block = "";
      
      $third_party_vouchers = "";
      
      $is_first_main_voucher = false;
      
        $ORDER_REPLACES = array(
        "[customer]"=>$order->userName,
        "[res_id]"=>$order->code,
        "[res_date_f1]"=>show_date_f1($order->date),
        "[res_date_f2]"=>show_date_f2($order->date),
        "[res_time]"=>show_time($order->date),
        "[affiliate]"=>$affiliate_name,
        "[agent]"=>$agent_name,        
        "[res_total]"=>$total
        );
         
        $order_part        = new OrderPartBO();
        $order_part->order = $order_id;
        if($order_part_id!="") $order_parts->id = $order_part_id;
        $order_part->Load(0,0);
        
        while($order_part->Next())
        {
            $item = new ItemBO();
            $item->id = $order_part->item;    
            $item->Load(0,0);    
            $item->Next();
            
            $order_part_total = 0;
            $order_part_discount = "";
            if($is_agent) //if agent - recalculate total amount for Voucher
            {
               $order_part_total = $order_part->adults_count * $item->adult_price + $order_part->childs_count * $item->child_price;
            }else
            {
               $order_part_total    = $order_part->total;
               $order_part_discount = $order_part->discount;
            }
            
            $adults  = $order_part->adults_count; 
            $kids    = $order_part->childs_count;
            if($item->only_for==0)
            {
              $persons = ($order_part->adults_count+0+$order_part->childs_count);   
            }elseif($item->only_for==1)
            {
              $persons = $order_part->adults_count;  
            }else
            {
              //$persons = $order_part->childs_count;
              $persons = $order_part->adults_count;  //kids count for meals stores in adult field
            }
             
            $item_comments   = $item->comments;
            $item_title      = $item->title;
            $child_age_range = $item->child_age_range;
            
            $ORDER_PART_REPLACES = array(
            "[item_title]"=>$item_title,
            "[item_description]"=>$order_part->descr,
            "[item_comments]"=>$item_comments,      
            "[item_code]"=>$order_part->itemCode,
            "[item_date]"=>show_date_f2($order_part->date),
            "[item_time]"=>$order_part->time,
            "[item_adults]"=>$adults,
            "[item_children]"=>$kids,
            "[item_persons]"=>$persons,            
            "[item_age_range]"=>$child_age_range,
            "[item_total]"=>$order_part_total,
            "[affiliate]"=>$affiliate_name,
            "[item_discount]"=>$order_part_discount
            );
      
            $voucher = new VoucherBO();
    	    $voucher->id = $item->voucher;
            $voucher->Load(0,1);    
            if($voucher->Next())
            {   
                $voucher_template = "";
      
                if($order_part->provider != $main_provider_id) //3rd party provider
                { 
                   $voucher_template = $voucher->content;
                }else //csny voucher will be 1
                {
                  if(!$is_first_main_voucher) //first CSNY voucher as BASE
                  {
                    $is_first_main_voucher = true; 
                    $main_provider_voucher = $voucher->content;  
                    $voucher_template      = $voucher->content;
                  }else
                  {
                    $voucher_template = $main_provider_voucher;
                  } 
                } 
 
                //get items block from BASE template            
                $items_block = $vtmpl->getBlock($voucher_template,"[items_begin]","[items_end]");    
                
                //get items block from current voucher
                $items_block_cur = $vtmpl->getBlock($voucher->content,"[items_begin]","[items_end]");    
                                
                $prepared_voucher = "";
                //if items block exist in BASE voucher and in current voucher
                if(strlen($items_block)>0 && strlen($items_block_cur)>0)
                {
                  //remove items block from original template
                  $new_template     = $vtmpl->cutBlock($voucher_template,"[items_begin]","[items_end]","[items_block]");   
                  
                  //prepared items block
                  $items_block      = $vtmpl->loadTemplateFromString($items_block_cur,$ORDER_PART_REPLACES);
                  
                  $ITEMS_BLOCK_REPLACES = array("[items_block]"=>$items_block);
      
                  //insert order info
                  $prepared_voucher = $vtmpl->loadTemplateFromString($new_template,$ORDER_REPLACES);
                  
                  //insert order part info
                  $prepared_voucher = $vtmpl->loadTemplateFromString($prepared_voucher,$ITEMS_BLOCK_REPLACES);
                }else
                {
                  //insert order info
                  $prepared_voucher = $vtmpl->loadTemplateFromString($voucher_template,$ORDER_REPLACES);
                }  
           
                if($order_part->provider != $main_provider_id) //3rd party provider
                { 
                    if($third_party_vouchers!="")
                    {
                       $third_party_vouchers.= "<p style='page-break-before:always'>".$prepared_voucher."</p>";     
                    }else
                    {
                       $third_party_vouchers.= "<p>".$prepared_voucher."</p>"; 
                    }                     
                }else //csny voucher will be 1
                {
                  $main_provider_voucher_items_block.=$items_block; 
                } 
         }

         $combos = new ComboBO();
         $combos->item = $item->id;
         $combos->Load(0,0);
         while($combos->Next())
         {
             $combo_item = new ItemBO();
             $combo_item->id = $combos->related;
             $combo_item->Load(0,1);
             if($combo_item->Next())
             {                 
                    $voucher = new VoucherBO();
                    $voucher->id = $combo_item->voucher;
                    $voucher->Load(0,1);    
                    if($voucher->Next())
                    {   
                            $voucher_template = "";
                            
                            $adults  = $order_part->adults_count; 
                            $kids    = $order_part->childs_count;
                            if($combo_item->only_for==0)
                            {
                              $persons = ($order_part->adults_count+0+$order_part->childs_count);   
                            }elseif($combo_item->only_for==1)
                            {
                              $persons = $order_part->adults_count;  
                            }else
                            {
                              $persons = $order_part->childs_count;
                            }
                            
                            if($persons==0) continue;
                             
                            $item_comments   = $combo_item->comments;
                            $item_title      = $combo_item->title;
                            $child_age_range = $combo_item->child_age_range;
                            
                            $ORDER_PART_REPLACES = array(
                            "[item_title]"=>$item_title,
                            "[item_comments]"=>$item_comments,      
                            "[item_code]"=>$order_part->itemCode,
                            "[item_date]"=>show_date_f2($order_part->date),
                            "[item_time]"=>$order_part->time,
                            "[item_adults]"=>$adults,
                            "[item_children]"=>$kids,
                            "[item_persons]"=>$persons,            
                            "[item_age_range]"=>$child_age_range,
                            );
              
                        if($combo_item->provider != $main_provider_id) //3rd party provider
                        { 
                           $voucher_template = $voucher->content;
                        }else //csny voucher will be 1
                        {
                          if(!$is_first_main_voucher) //first CSNY voucher as BASE
                          {
                            $is_first_main_voucher = true;
                            $main_provider_voucher = $voucher->content;  
                            $voucher_template      = $voucher->content;
                          }else
                          {
                            $voucher_template = $main_provider_voucher;
                          } 
                        } 
         
                        //get items block from BASE template            
                        $items_block = $vtmpl->getBlock($voucher_template,"[items_begin]","[items_end]");    
                        
                        //get items block from current voucher
                        $items_block_cur = $vtmpl->getBlock($voucher->content,"[items_begin]","[items_end]");    
                                        
                        $prepared_voucher = "";
                        //if items block exist in BASE voucher and in current voucher
                        if(strlen($items_block)>0 && strlen($items_block_cur)>0)
                        {
                          //remove items block from original template
                          $new_template     = $vtmpl->cutBlock($voucher_template,"[items_begin]","[items_end]","[items_block]");   
                          
                          //prepared items block
                          $items_block      = $vtmpl->loadTemplateFromString($items_block_cur,$ORDER_PART_REPLACES);
                          
                          $ITEMS_BLOCK_REPLACES = array("[items_block]"=>$items_block);
              
                          //insert order info
                          $prepared_voucher = $vtmpl->loadTemplateFromString($new_template,$ORDER_REPLACES);
                          
                          //insert order part info
                          $prepared_voucher = $vtmpl->loadTemplateFromString($prepared_voucher,$ITEMS_BLOCK_REPLACES);
                        }else
                        {
                          //insert order info
                          $prepared_voucher = $vtmpl->loadTemplateFromString($voucher_template,$ORDER_REPLACES);
                        }  
                   
                        if($combo_item->provider != $main_provider_id) //3rd party provider
                        { 
                            if($third_party_vouchers!="")
                            {
                               $third_party_vouchers.= "<p style='page-break-before:always'>".$prepared_voucher."</p>";     
                            }else
                            {
                               $third_party_vouchers.= "<p>".$prepared_voucher."</p>"; 
                            }                            
                            //$third_party_vouchers.= "<p style='page-break-after:always'>".$prepared_voucher."</p>"; 
                        }else //csny voucher will be 1
                        {
                          $main_provider_voucher_items_block.=$items_block; 
                        } 
                }
             }       
         }
    }

        //prepare main provider voucher
        if($order->provider==$main_provider_id)
        {
            $voucher_template = $main_provider_voucher;
            $items_block = $vtmpl->getBlock($voucher_template,"[items_begin]","[items_end]");    
            
            $prepared_voucher = "";
            if(strlen($items_block)>0)
            {
              //remove items block from original template
              $new_template     = $vtmpl->cutBlock($voucher_template,"[items_begin]","[items_end]","[items_block]");   
              
              //prepared items block
              $ITEMS_BLOCK_REPLACES = array("[items_block]"=>$main_provider_voucher_items_block);
      
              //insert order info
              $prepared_voucher = $vtmpl->loadTemplateFromString($new_template,$ORDER_REPLACES);
              
              //insert order part info
              $prepared_voucher = $vtmpl->loadTemplateFromString($prepared_voucher,$ITEMS_BLOCK_REPLACES);
            }else
            {
              //insert order info
              $prepared_voucher = $vtmpl->loadTemplateFromString($voucher_template,$ORDER_REPLACES);
            }  
            $HTML .= "<p>".$prepared_voucher."</p>";
        }
        
        if(strlen($third_party_vouchers)>0)
        {
          //$HTML .= "<p style='page-break-after:always'>".$prepared_voucher."</p>";
          //$HTML .= "<br>=============================================================</br>";
          if($HTML!="")
          {
            $HTML .= "<p style='page-break-before:always;margin:0;'></p>".$third_party_vouchers;    
          }else
          {
            $HTML .= $third_party_vouchers;
          }
        }
    }else
    {
      $HTML .= "<center><b>Sorry, but reservation was not found.</b></center>";
    }
    return $HTML;
  }
     
  function stringPDF()
  {
      return $this->pdf->getPdfStream();
  }

  function streamPDF()
  {
      $this->pdf->stream();
  }

  function getVouchersAsPDF($vouchers)
  {
      //$this->pdf = new Cezpdf('a4','portrait');
      $this->pdf = new Cezpdf('a4','portrait');      
      $this->pdf->ezSetMargins(0,0,0,0);
      $first = true;
      $this->top_y = $this->pdf->y;
      foreach($vouchers as $value)
      {
        $voucher_file = PROJECT_ROOT.VOUCHERS_TMP.$value;  
        if(file_exists($voucher_file))
        {
            if($first)
            {
                $first = false;                
            }else
            {
                //add new page
                $this->pdf->ezNewPage();
            }

            $size=getimagesize($voucher_file);
		    
            $w=$size[0]; //real width
		    $h=$size[1]; //real height
		    
		    $h_   = $this->a4_w*$h/$w;
		    
		    $this->pdf->addJpegFromFile($voucher_file, 0, $this->pdf->ez['pageHeight']-$h_,$this->a4_w);
		    //$this->pdf->addJpegFromFile($voucher_file, 0, $this->pdf->ez['pageHeight']-$h,$w,$h);
	    }
      }
  }
  
  function getOrderVouchersAsImages($order_id,$user_id,$order_part_id="")
  {
      $vouchers   = array(); //vouchers container...e.g. result array of filenames
      
      $is_agent     	  = false;
      $is_inhouse_agent   = false;
      $is_concierge       = false;
      $agent_name         = "-";
      $affiliate_name     = "";
      
      $user     = new UserBO();
      $user->id = $user_id;
      $user->Load(0,1);
      if($user->Next())
      {
         if($user->agn_agent==1)
         {
             $is_agent = true;
             $agent_name = $user->fname." ".$user->lname;             
             if($user->inhome==1)
   	     {
        	    $is_inhouse_agent = true;   
	     }	
         }
      }

      //load order
      $order         = new OrderBO();
      $order->id     = $order_id;
      /*
      if($is_agent)
      {
        $order->agentID = $user_id;  
      }else
      {
        $order->user   = $user_id;    
      } 
      */     
      $order->Load(0,1);
      if($order->Next())
      {
          if($order->affiliate_id !="0")
          {
            $affiliate      = new AffiliateBO();
            $affiliate->id  = $order->affiliate_id;
    
            $affiliate->Load(0,1);
            if($affiliate->Next())
            {
    	      $affiliate_name = $affiliate->firstName." ".$affiliate->lastName;
    	      $is_concierge = $affiliate->is_concierge == 1 ? true : false;
            }
          }
          
          $bottompart_file = PROJECT_ROOT.VOUCHERS."bottompart_voucher.jpg";
          if($is_concierge)
          {
              $bottompart_file = PROJECT_ROOT.VOUCHERS."bottompart_concierge.jpg";
          }
          
          $total = 0;
          if($is_agent && !$is_inhouse_agent) //if agent - recalculate total amount for Voucher
          {
            $order_parts = new OrderPartBO();
            $order_parts->order = $order_id;
            if($order_part_id!="") $order_parts->id = $order_part_id;
            $order_parts->Load(0,0);
    
            while($order_parts->Next())
            {
                $item = new ItemBO();
                $item->id = $order_parts->item;    
                $item->Load(0,1);    
                $item->Next();
                $total += $order_parts->adults_count * $item->adult_price + $order_parts->childs_count * $item->child_price;
            }
          }else
          {
             $total = $order->total;
          }
          
          $order_part        = new OrderPartBO();
          $order_part->order = $order_id;
          if($order_part_id!="") $order_parts->id = $order_part_id;
          $order_part->Load(0,0);
          while($order_part->Next())
          {
                $item = new ItemBO();
                $item->id = $order_part->item;    
                $item->Load(0,1);    
                $item->Next();
                
                $order_part_total    = 0;
                $order_part_discount = "";
                if($is_agent && !$is_inhouse_agent) //if agent - recalculate total amount for Voucher
                {
                   $order_part_total = $order_part->adults_count * $item->adult_price + $order_part->childs_count * $item->child_price;
                }else
                {
                   $order_part_total    = $order_part->total;
                   $order_part_discount = $order_part->discount;
                }
                
                $adults  = $order_part->adults_count; 
                $kids    = $order_part->childs_count;
                if($item->only_for==0)
                {
                  $persons = ($order_part->adults_count+0+$order_part->childs_count);   
                }elseif($item->only_for==1)
                {
                  $persons = $order_part->adults_count;  
                }else
                {
                  $persons = $order_part->adults_count;  //kids count for meals stores in adult field
                }

                $voucher = new VoucherBO();

                $balance_due = "";
                if($order->cash_deposit=="1" && $order->balance_due > 0)
                {
                    $balance_due = "Balance Due: $".$this->formatMoney($order->balance_due);
                    $voucher->id = $this->default_voucher;     
                }else
                {
                    $voucher->id = $item->voucher;    
                }

                /*
                $log = new LogsBO();
                $log->write(0,"vovik","itemID:".$item->id." | orderID:".$order->id." | voucherID:".$voucher->id);
                */
                
    	        $voucher->vtype = 1;  //load only IMAGE vouchers
                $voucher->Load(0,1);
                
                if($voucher->Next())
                {
                  $voucher_file    = PROJECT_ROOT.VOUCHERS.$voucher->content;
                  if(file_exists($voucher_file)) //if file not exist - create new one
                  {
                    $REPLACES = array(
                        "customer"          => $order->userName,
                        "res_id"            => $order->code,
                        "res_date_f1"       => show_date_f1($order->date),
                        "res_date_f2"       => show_date_f2($order->date),
                        "res_time"          => show_time($order->date),
                        "affiliate"         => $affiliate_name,
                        "agent"             => $agent_name,        
                        "res_total"         => "$".$this->formatMoney($total),
                        "item_title"        => $item->title,
                        "item_description"  => $order_part->descr,
                        "item_comments"     => $item->comments,      
                        "item_code"         => $order_part->itemCode,
                        "item_date"         => show_date_f2($order_part->date),
                        "item_time"         => $order_part->time,
                        "item_adults"       => $adults,
                        "item_children"     => $kids,
                        "item_persons"      => $persons,            
                        "item_age_range"    => $item->child_age_range,
                        "item_total"        => "$".$this->formatMoney($order_part_total),
                        "item_discount"     => $order_part_discount,
                        "balance_due"       => $balance_due,
                    	"language"  	    => $order_part->language                    
                    );
                    
                    $name    = uniqid(rand()).".jpg";
                    $save_to = PROJECT_ROOT.VOUCHERS_TMP.$name;
                                        
                    $this->drawVoucher($voucher->id,$voucher_file,$REPLACES,$save_to,$bottompart_file);
                    $vouchers[] = $name;
                  }            
                }else
                {
                   continue;
                } 
                
                $combos       = new ComboBO();
                $combos->item = $item->id;
                $combos->Load(0,0);
                while($combos->Next())
                {
                     $combo_item     = new ItemBO();
                     $combo_item->id = $combos->related;
                     $combo_item->Load(0,1);
                     if($combo_item->Next())
                     {                 
                            $voucher        = new VoucherBO();
                            $voucher->id    = $combo_item->voucher;
                            $voucher->vtype = 1;  //only IMAGE type
                            $voucher->Load(0,1);    
                            if($voucher->Next())
                            {   
                                  $voucher_file    = PROJECT_ROOT.VOUCHERS.$voucher->content;                                  
                                  if(file_exists($voucher_file)) //if file not exist - create new one
                                  {
                                    $adults  = $order_part->adults_count; 
                                    $kids    = $order_part->childs_count;
                                    if($combo_item->only_for==0)
                                    {
                                      $persons = ($order_part->adults_count+0+$order_part->childs_count);   
                                    }elseif($combo_item->only_for==1)
                                    {
                                      $persons = $order_part->adults_count;  
                                    }else
                                    {
                                      $persons = $order_part->childs_count;
                                    }
                                    if($persons==0) continue;
                                      
                                    $REPLACES = array(
                                        "customer"          => $order->userName,
                                        "res_id"            => $order->code,
                                        "res_date_f1"       => show_date_f1($order->date),
                                        "res_date_f2"       => show_date_f2($order->date),
                                        "res_time"          => show_time($order->date),
                                        "affiliate"         => $affiliate_name,
                                        "agent"             => $agent_name,        
                                        "res_total"         => "$".$total,
                                        "item_title"        => $combo_item->title,
                                        "item_description"  => $order_part->descr,
                                        "item_comments"     => $combo_item->comments,      
                                        "item_code"         => $order_part->itemCode,
                                        "item_date"         => show_date_f2($order_part->date),
                                        "item_time"         => $order_part->time,
                                        "item_adults"       => $adults,
                                        "item_children"     => $kids,
                                        "item_persons"      => $persons,            
                                        "item_age_range"    => $combo_item->child_age_range,
                                        "item_total"        => "$".$order_part_total,
                                        "item_discount"     => $order_part_discount,
                                    	"language"  	    => $order_part->language
                                    );
                                    
                                    $name    = uniqid(rand()).".jpg";
                                    $save_to = PROJECT_ROOT.VOUCHERS_TMP.$name;                                    
                                    $this->drawVoucher($voucher->id,$voucher_file,$REPLACES,$save_to,$bottompart_file);                                    
                                    $vouchers[] = $name;
                                  }
                             }       
                         }
                  }//combos while          
           }//order parts while
      }//order
      return $vouchers;    
  }
  
  function drawVoucher($voucher_id,$voucher_file,$REPLACES,$save_to,$bottompart_file="")
  {
      $ie = new ImageEditor();
      $ie->loadImage($voucher_file);

      $comments_code = "item_comments";
      $barcode       = "barcode";
      
      $vfield = new VoucherFieldsBO();
	  $vfield->voucher = $voucher_id;
	  $vfield->Load(0,0);	  
	  while($vfield->Next())
	  {	      	     
	    if(isset($REPLACES[$vfield->code]) && trim($REPLACES[$vfield->code])!="")
	    {	        
	      if($vfield->code == $comments_code)
	      {
	        $comments_code_val = "".$REPLACES[$comments_code];
	        $pos = strpos($comments_code_val,$barcode);
	        	           	        
	          if($pos===false)
	          {
	              //not found "barcode" word
	          }else
	          {
	              //found "barcode" word...so draw barcode
	              $ie->addImage(PROJECT_ROOT.$comments_code_val,$vfield->x,$vfield->y);
	              continue;
	          }
	      }
	      
	      $font = $vfield->font; 
	      if($vfield->bold==1)
	      {
	         $font = $font."b";    
	      }
	      $font = $font.".ttf";
      
          $ie->setFont(PROJECT_ROOT.VOUCHERS."fonts/".$font);        
          $ie->setSize($vfield->size);
          $rgb = $ie->html2rgb($vfield->color);     

          $ie->addText($REPLACES[$vfield->code], $vfield->x,$vfield->y, 0, $rgb[0],$rgb[1],$rgb[2]);
	    }
	  }
	  
	  if($ie->h < 1000 && $bottompart_file!="" && file_exists($bottompart_file))
	  {
	      $ie_bg = new ImageEditor();
          $bottompart_img = $ie_bg->loadImageFromFile($bottompart_file);
          if(!$bottompart_img)
          {
             //skip it          
          }else
          {
             $bottompart_img_w = $ie_bg->getImageWidth($bottompart_img);
             $bottompart_img_h = $ie_bg->getImageHeight($bottompart_img);
             
             $ie_bg->createImage($ie->w,$ie->h+$bottompart_img_h);
             $ie_bg->copyImage($ie->img,0,0,0,0,$ie->w,$ie->h);
             $ie_bg->copyImage($bottompart_img,0,$ie->h,0,0,$bottompart_img_w,$bottompart_img_h);        
             $ie_bg->outputFile($save_to,"jpg");
             return;
          }
	  }
	  
	  $ie->outputFile($save_to,"jpg");
	  return;    
  }  
  
  function getOrderLetterOfAuth($order_id,$user_id,$voucher_title)
  {
      $is_agent       = false;
      $agent_name     = "-";
      $affiliate_name = "";
      $prepared_voucher = "";
      
      $user     = new UserBO();
      $user->id = $user_id;
      $user->Load(0,1);
      if($user->Next())
      {
         if($user->agn_agent==1)
         {
             $is_agent = true;
             $agent_name = $user->fname." ".$user->lname;             
         }
      }
      
      //load order
      $order         = new OrderBO();
      $order->id     = $order_id;
      if($is_agent)
      {
        $order->agentID = $user_id;  
      }else
      {
        $order->user   = $user_id;    
      }
            
      $order->Load(0,1);
      if($order->Next())
      {
          $total = 0;
          if($is_agent) //if agent - recalculate total amount for Voucher
          {
            $order_parts = new OrderPartBO();
            $order_parts->order = $order_id;
            if($order_part_id!="") $order_parts->id = $order_part_id;
            $order_parts->Load(0,0);
    
            while($order_parts->Next())
            {
                $item = new ItemBO();
                $item->id = $order_parts->item;    
                $item->Load(0,0);    
                $item->Next();
                $total += $order_parts->adults_count * $item->adult_price + $order_parts->childs_count * $item->child_price;
            }
          }else
          {
             $total = $order->total;
          }
          
          $order_part        = new OrderPartBO();
          $order_part->order = $order_id;
          //if($order_part_id!="") $order_parts->id = $order_part_id;
          $order_part->Load(0,0);

          $items_list = "";
          while($order_part->Next())
          {
                $item = new ItemBO();
                $item->id = $order_part->item;    
                $item->Load(0,0);    
                if($item->Next())
                {
                   $items_list = $items_list.$item->title."<br>";                    
                }
                
                $combos       = new ComboBO();
                $combos->item = $item->id;
                $combos->Load(0,0);
                while($combos->Next())
                {
                     $combo_item     = new ItemBO();
                     $combo_item->id = $combos->related;
                     $combo_item->Load(0,1);
                     if($combo_item->Next())
                     {                 
                        $items_list = $items_list.$combo_item->title."<br>";
                     }
                }//combos while          
           }//order parts while
   
           $ORDER_REPLACES = array(
            "[customer]"          => $order->userName,
            "[res_id]"            => $order->code,
            "[res_date_f1]"       => show_date_f1($order->date),
            "[res_date_f2]"       => show_date_f2($order->date),
            "[res_time]"          => show_time($order->date),
            "[agent]"             => $agent_name,        
            "[res_total]"         => $total,
            "[cc_number]"         => substr_replace($order->CCNumber, '************', 0, (strlen($order->CCNumber)-4)),
            "[cc_cvv]"            => $order->CVV2,
            "[cc_type]"           => $order->CCType,
            "[cc_expdate]"        => $order->CCExpireMonth."/".$order->CCExpireYear           
            );

            $ORDER_PART_REPLACES = array(
            "[item_title]"=>$items_list
            );
        
            $vtmpl = new Template();
            
            $voucher = new VoucherBO();
    	    $voucher->title = $voucher_title;
            $voucher->Load(0,1);    
            if($voucher->Next())
            {                   
                $voucher_template = $voucher->content;  
                //get items block from current voucher
                $items_block = $vtmpl->getBlock($voucher_template,"[items_begin]","[items_end]");    
                                               
                //if items block exist in BASE voucher and in current voucher
                if(strlen($items_block)>0)
                {
                  //remove items block from original template
                  $new_template = $vtmpl->cutBlock($voucher_template,"[items_begin]","[items_end]","[items_block]");   
                  
                  //prepared items block
                  $items_block      = $vtmpl->loadTemplateFromString($items_block,$ORDER_PART_REPLACES);
                  
                  $ITEMS_BLOCK_REPLACES = array("[items_block]"=>$items_block);
      
                  //insert order info
                  $prepared_voucher = $vtmpl->loadTemplateFromString($new_template,$ORDER_REPLACES);
                  
                  //insert order part info
                  $prepared_voucher = $vtmpl->loadTemplateFromString($prepared_voucher,$ITEMS_BLOCK_REPLACES);
                }else
                {
                  //insert order info
                  $prepared_voucher = $vtmpl->loadTemplateFromString($voucher_template,$ORDER_REPLACES);
                }  
             }                      
      }//order  

      return $prepared_voucher;
  }
  
  function getOrderLetterOfAuthAsImage($order_id,$user_id,$voucher_title)
  {
      $result_image = "";      
      
      $is_agent       = false;
      $agent_name     = "-";
      $affiliate_name = "";
      $prepared_voucher = "";
      
      $user     = new UserBO();
      $user->id = $user_id;
      $user->Load(0,1);
      if($user->Next())
      {
         if($user->agn_agent==1)
         {
             $is_agent = true;
             $agent_name = $user->fname." ".$user->lname;             
         }
      }
      
      //load order
      $order         = new OrderBO();
      $order->id     = $order_id;
      if($is_agent)
      {
        $order->agentID = $user_id;  
      }else
      {
        $order->user   = $user_id;    
      }
            
      $order->Load(0,1);
      if($order->Next())
      {
          $total = 0;
          if($is_agent) //if agent - recalculate total amount for Voucher
          {
            $order_parts = new OrderPartBO();
            $order_parts->order = $order_id;
            //if($order_part_id!="") $order_parts->id = $order_part_id;
            $order_parts->Load(0,0);
    
            while($order_parts->Next())
            {
                $item = new ItemBO();
                $item->id = $order_parts->item;    
                $item->Load(0,0);    
                $item->Next();
                $total += $order_parts->adults_count * $item->adult_price + $order_parts->childs_count * $item->child_price;
            }
          }else
          {
             $total = $order->total;
          }
          
          $order_part        = new OrderPartBO();
          $order_part->order = $order_id;
          //if($order_part_id!="") $order_parts->id = $order_part_id;
          $order_part->Load(0,0);

          $items_list = "";
          $isfirst = true;
          while($order_part->Next())
          {
                $item = new ItemBO();
                $item->id = $order_part->item;    
                $item->Load(0,0);    
                if($item->Next())
                {
                   if($isfirst)
                   {                       
                       $isfirst = false;
                       $items_list = $item->title;     
                   }else
                   {
                       $items_list = $items_list.",".$item->title;   
                   }                                       
                }
                
                $combos       = new ComboBO();
                $combos->item = $item->id;
                $combos->Load(0,0);
                while($combos->Next())
                {
                     $combo_item     = new ItemBO();
                     $combo_item->id = $combos->related;
                     $combo_item->Load(0,1);
                     $isfirst = true;
                     if($combo_item->Next())
                     {
                        $items_list = $items_list.",".$combo_item->title;    
                     }
                }//combos while          
           }//order parts while
   
            $REPLACES = array(
                "customer"          => $order->userName,
                "res_id"            => $order->code,
                "res_date_f1"       => show_date_f1($order->date),
                "res_date_f2"       => show_date_f2($order->date),
                "res_time"          => show_time($order->date),
                "affiliate"         => $affiliate_name,
                "agent"             => $agent_name,
                "is_concierge"      => "1", 
                "cc_name"           => $order->CCNameOnCard,       
                "cc_number"         => substr_replace($order->CCNumber, '************', 0, (strlen($order->CCNumber)-4)),
                "cc_cvv"            => $order->CVV2,
                "cc_type"           => $order->CCType,
                "cc_expdate"        => $order->CCExpireMonth."/".$order->CCExpireYear,
                "item_title"        => $items_list,
                "res_total"         => "$".$this->formatMoney($total)
            );
           
            $vtmpl = new Template();
            
            $voucher = new VoucherBO();
    	    $voucher->title = $voucher_title;
            $voucher->Load(0,1);    
            if($voucher->Next())
            {
                $voucher_file    = PROJECT_ROOT.VOUCHERS.$voucher->content;
                if(file_exists($voucher_file)) //if file not exist - create new one
                {                
                    $name    = uniqid(rand()).".jpg";
                    $save_to = PROJECT_ROOT.VOUCHERS_TMP.$name;
                    $this->drawVoucher($voucher->id,$voucher_file,$REPLACES,$save_to,"");
                    $result_image = $name;
                }                  
            }                      
      }//order  

      return $result_image;
  }

  function formatMoney($number) 
  { 
    if ($number==0) { // zero
          $money = "0.00"; // output zero
    } else { // value
      if (floor($number) == $number) 
      { // whole number
        $money = $number.".00"; // format        
      } else { // cents
        $money = number_format(round($number, 2), 2); // format
      } // integer or decimal
    } // value
    return $money;
  }
}
?>